import axios, { InternalAxiosRequestConfig, AxiosError, AxiosResponse } from "axios";
import { message } from 'antd'
axios.defaults.baseURL = 'http://127.0.0.1:3005/api'
axios.defaults.timeout = 5000
axios.interceptors.request.use((config: InternalAxiosRequestConfig) => {
    let token = localStorage.getItem('token')
    if (token) {

        config.headers['Authorization'] = 'Bearer ' + token

    }
    return config
})
axios.interceptors.response.use((response: AxiosResponse) => {
    return response
}, (error: AxiosError
) => {
    switch (error.response?.status) {
        case 500:
            message.warning(`服务端出现了500错误`)
            break
        case 404:
            message.warning(`服务端出现了404错误`)
            break
        case 401:
            message.warning(`服务端出现了401错误`)
            // localStorage.removeItem('token')
            break
    }
    return Promise.reject(error)
})
export default axios